import tkinter as tk
from tkinter import messagebox
from datetime import datetime

class TodoItem:
    def __init__(self, name, start_time, end_time, reminder):
        self.name = name
        self.start_time = start_time
        self.end_time = end_time
        self.reminder = reminder

class TodoApp:
    def __init__(self, root):
        self.root = root
        self.root.title("待办事项管理系统")

        # 待办事项列表
        self.todo_list = []

        # 创建主窗口组件
        self.create_widgets()

    def create_widgets(self):
        # 搜索栏
        self.search_label = tk.Label(self.root, text="搜索:")
        self.search_label.grid(row=0, column=0)
        self.search_entry = tk.Entry(self.root)
        self.search_entry.grid(row=0, column=1)

        # 待办事项列表
        self.todo_listbox = tk.Listbox(self.root, width=50, height=15)
        self.todo_listbox.grid(row=1, column=0, columnspan=2)

        # 编辑按钮
        self.edit_button = tk.Button(self.root, text="编辑事项", command=self.edit_todo)
        self.edit_button.grid(row=2, column=0)

        # 添加按钮
        self.add_button = tk.Button(self.root, text="添加事项", command=self.add_todo)
        self.add_button.grid(row=2, column=1)

        # 删除按钮
        self.delete_button = tk.Button(self.root, text="删除事项", command=self.delete_todo)
        self.delete_button.grid(row=3, column=0)

        # 完成按钮
        self.complete_button = tk.Button(self.root, text="标记事项已完成", command=self.mark_completed)
        self.complete_button.grid(row=3, column=1)

    def add_todo(self):
        # 跳转编辑待办事项窗口
        edit_window = tk.Toplevel(self.root)
        edit_window.title("编辑待办事项")

        # 编辑窗口组件
        tk.Label(edit_window, text="事项名称:").grid(row=0, column=0)
        tk.Label(edit_window, text="开始时间:").grid(row=1, column=0)
        tk.Label(edit_window, text="截止时间:").grid(row=2, column=0)
        tk.Label(edit_window, text="提醒内容:").grid(row=3, column=0)

        name_entry = tk.Entry(edit_window)
        name_entry.grid(row=0, column=1)
        start_entry = tk.Entry(edit_window)
        start_entry.grid(row=1, column=1)
        end_entry = tk.Entry(edit_window)
        end_entry.grid(row=2, column=1)
        reminder_entry = tk.Entry(edit_window)
        reminder_entry.grid(row=3, column=1)

        # 保存按钮
        save_button = tk.Button(edit_window, text="保存", command=lambda: self.save_todo(name_entry.get(), start_entry.get(), end_entry.get(), reminder_entry.get(), edit_window))
        save_button.grid(row=4, column=0, columnspan=2)

    def save_todo(self, name, start, end, reminder, edit_window):
        # 保存事项到列表
        todo_item = TodoItem(name, start, end, reminder)
        self.todo_list.append(todo_item)
        self.update_todo_listbox()
        edit_window.destroy()

    def edit_todo(self):
        # 编辑选中的事项
        selected_index = self.todo_listbox.curselection()
        if selected_index:
            todo_item = self.todo_list[selected_index[0]]

            # 创建编辑窗口
            edit_window = tk.Toplevel(self.root)
            edit_window.title("编辑待办事项")

            # 填充编辑窗口
            tk.Label(edit_window, text="事项名称:").grid(row=0, column=0)
            tk.Label(edit_window, text="开始时间:").grid(row=1, column=0)
            tk.Label(edit_window, text="截止时间:").grid(row=2, column=0)
            tk.Label(edit_window, text="提醒内容:").grid(row=3, column=0)

            name_entry = tk.Entry(edit_window)
            name_entry.grid(row=0, column=1)
            name_entry.insert(0, todo_item.name)
            start_entry = tk.Entry(edit_window)
            start_entry.grid(row=1, column=1)
            start_entry.insert(0, todo_item.start_time)
            end_entry = tk.Entry(edit_window)
            end_entry.grid(row=2, column=1)
            end_entry.insert(0, todo_item.end_time)
            reminder_entry = tk.Entry(edit_window)
            reminder_entry.grid(row=3, column=1)
            reminder_entry.insert(0, todo_item.reminder)

            # 更新按钮
            update_button = tk.Button(edit_window, text="保存", command=lambda: self.update_todo(selected_index[0], name_entry.get(), start_entry.get(), end_entry.get(), reminder_entry.get(), edit_window))
            update_button.grid(row=4, column=0, columnspan=2)

    def update_todo(self, index, name, start, end, reminder, edit_window):
        # 更新事项信息
        self.todo_list[index].name = name
        self.todo_list[index].start_time = start
        self.todo_list[index].end_time = end
        self.todo_list[index].reminder = reminder
        self.update_todo_listbox()
        edit_window.destroy()

    def delete_todo(self):
        # 删除选中的事项
        selected_index = self.todo_listbox.curselection()
        if selected_index:
            del self.todo_list[selected_index[0]]
            self.update_todo_listbox()

    def mark_completed(self):
        # 标记选中的事项已完成
        selected_index = self.todo_listbox.curselection()
        if selected_index:
            self.todo_listbox.itemconfig(selected_index[0], {'fg': 'red'})

    def update_todo_listbox(self):
        # 更新待办事项列表框
        self.todo_listbox.delete(0, tk.END)
        for item in self.todo_list:
            self.todo_listbox.insert(tk.END, item.name)

    def check_reminders(self):
        # 检查提醒
        now = datetime.now().strftime("%Y-%m-%d %H:%M")
        for item in self.todo_list:
            if item.end_time == now:
                messagebox.showinfo("提醒", f"事项 '{item.name}' 的提醒内容: {item.reminder}")

    def run(self):
        # 运行主程序
        self.root.mainloop()

# 创建主窗口
root = tk.Tk()
app = TodoApp(root)

# 定时检查提醒
# 注意：这里只是简单的演示，实际应用中可能需要更复杂的实现，比如使用多线程或定时器来实现后台运行
app.check_reminders()

# 运行程序
app.run()